/******************************************************************************
Filename:     rf_test.c
Target:       cc2430
Revised:      16/12-2005
Revision:     1.0

Description:
    This file provides 4 small tests which demonstrates use of the radio.

******************************************************************************/

#include "ioCC2430.h"
#include "cul.h"
#include "hal.h"
#include "RF04EB.h"
#include "bsp.h"
#include "string.h"

#define SINGLE      0
#define CONTINUOUS  1
#define PING_PONG   2
#define PER_TEST    3
#define EXIT        4

#define RECEIVE_TIMEOUT               800

#define PING_PONG_TIMEOUT             1200
#define PING_PONG_REQUEST             0x80
#define PING_PONG_RESPONSE            0x40

#define PER_RECEIVE_TIMEOUT           10000
#define PER_TOTAL_PACKET_NUMBER       1000
#define PER_TEST_REQUEST              0x20



#define send

/////////////////////////////////////////////////////////////////////////////////////
/*function  */
/////////////////////////////////////////////////////////////////////////////////////

void initRf(void);

UINT8 myAddr;
uint8 write_flag=0;
uint8 uart_buf[30];
uint8 uart_len;
/******************************************************************************
* @fn  initRf
*
* @brief
*      Initializes components for the RF test application example.
*
* Parameters:
*
* @param  void
*
* @return void
*
******************************************************************************/
void initRf(void)
{
   UINT32 frequency = 2405000;

   INIT_LED1();
   INIT_LED3();
   radioInit(frequency, myAddr);
}


/******************************************************************************
* @fn  main
*
* @brief
*      Main function.
*
* Parameters:
*
* @param  void
*
* @return void
*
******************************************************************************/
void main (void)
{
  BYTE* receiveBuf;
  uint8 len;
  uint8 sender;
  uint8 fcs_value;
  uint8 send_data[16] = {0x02,0x00,0x17,0x0B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

  INT_GLOBAL_ENABLE(INT_ON);
  myAddr = ADDRESS_0;
  initRf();
  HalUARTInit();

  halWait(50);
  send_data[1] = NODE_ID;
//LED1_TOGGLE();
       //LED1_TOGGLE();
   while(1)
   {
     INT_GLOBAL_ENABLE(INT_ON);
      if (TRUE == radioReceive(&receiveBuf, &len, RECEIVE_TIMEOUT, &sender))
      {
        if(sender == 0x01)
        {
          fcs_value= 0;
          for(uint8 i=1;i<len-1;i++)
          {
            fcs_value ^= receiveBuf[i];
          }
          if (len && (receiveBuf[0] == 0x02) && (fcs_value == (receiveBuf[len-1])))
          {
            LED1_TOGGLE();
            //LED2_TOGGLE();
            if(receiveBuf[2] == 0x17)
            {
              for(uint8 i= 0;i<(len-5)/11;i++)
              {
                memcpy(&send_data[4],&receiveBuf[11*i+4],11);
                  send_data[15] = 0;
                  for(uint8 j=1;j<15;j++)
                  {
                    send_data[15] ^= send_data[j];
                  }
                UartTX_Send_String0(send_data,16);
              }
            }
            else
            {
              UartTX_Send_String0(receiveBuf,len);
            }
          }
        }
      }
      //UartTX_Send_String0(a_page_data,20);
      //halWait(20);

      if(write_flag == 1 )
      {
        LED2_TOGGLE();
       radioSend(uart_buf, uart_len, ADDRESS_1);
       memset(uart_buf,0,30);
       uart_len = 0;
        write_flag = 0;
      }
  }
}
